var content = require('../../../public/content');
var suggest = require('./suggest');
var request = require('request');
var template = require('template');

return extend(content, {
    target:'#general-ledger',
    path:'/general-ledger/',
    data:{
        title:'总账',
        exportsUrl:'http://www.baidu.com',
        printUrl:'',
        istwo:true
    },
    events:{
        'click .m-main-header .j-prev':'prev',
        'click .m-main-header .j-next':'next',
        'focus .m-main-header .dropdown':'dropdown'
    },
    template:{
        left:renders({
            <%include 'date'%>
            <%include 'subject'%>
        }),
        right:renders({
            <button class="ui-button">生成年度账簿</button>
            <button class="ui-button ui-button-normal e-ml15">联查明细账</button>
            <%include 'print'%>
            <%include 'exports'%>
        })
    },
    grid:{
        isPaging:false,
        paging:{
            url:sourceHttp,
            isPage:false
        },
        columns:[{
            title:'期间',
            width:'120',
            field:'buname'
        }, {
            title:'摘要',
            field:'buname',
            align:'left'
        }, {
            title:'借方',
            width:'20%',
            field:'buname',
            align:'right'
        }, {
            title:'贷方',
            width:'20%',
            field:'buname',
            align:'right'
        }, {
            title:'方向',
            width:'50',
            field:'buname',
            align:'center',
            filter:function(){
                return '借'
            }
        }, {
            title:'余额',
            width:'20%',
            field:'buname',
            align:'right'
        }]
    },
    onChange:function(){
        var that = this;
        var tpl = this.template;
        var data = that.data;
        content.onChange.call(this);
        request.get('subject.json', {}, {
            200:function(res){
                data.subjects = res.data;
                data.subject = res.data[0] || {};
                that.header.find('.subject').replaceWith(template.render(tpl.subject, that.data));
                that._query();
            }
        })
    },
    onInit:function(self){
        content.onInit.call(this, self);
        this.data.subjects = [];
        this.data.subject = {};
        this.data.subject_index = 0;
    },
    dropdown:function(e, elem){
        suggest.call(this, elem, this.data.subjects)
    },
    _query:function(elem){
        this.setTitle((this.data.subject.name || '') + this.data.title);
        if(elem){
            elem.siblings('input:hidden').val(this.data.subject.id);
            elem.siblings(':text').val(this.data.subject.buname);
        }
        this.query();
    },
    prev:function(e, elem){
        if(this.data.subject_index === 0){
            return false
        }
        this.data.subject_index -= 1;
        this.data.subject = this.data.subjects[this.data.subject_index];
        this._query(elem);
    },
    next:function(e, elem){
        if(this.data.subject_index >= this.data.subjects.length-1){
            return false
        }
        this.data.subject_index += 1;
        this.data.subject = this.data.subjects[this.data.subject_index];
        this._query(elem);
    }
})